{-include file=$smarty.const.B_DIR|cat:'/src/_header.htm'-}


<script type="text/javascript">
Ext.onReady(function() {
	Ext.QuickTips.init();
try{
	var Plant = Ext.data.Record.create([
		{name :'id', type: 'int'},
		{name :'单位'},
		{name :'采食量单位'},
		{name:'换算比率'},
		{name :'采食系数'},
		{name :'备注'}
	]);


	var store = new Ext.data.JsonStore({
		url: '?act=list',
		root:'root',
		id:'id',
		fields: ['id', '单位', '采食量单位', '采食系数','换算比率', '备注' ]
	
	});

	var sm = new Ext.grid.CheckboxSelectionModel(); 

	var fm = Ext.form;
	var cm = new Ext.grid.ColumnModel([
		//sm,
		{
		   header: "ID",
		   dataIndex: 'id',
		   width: 60
		},
		{
		   header: "单位",
		   dataIndex: '单位',
		   width: 130,
		   editor:  new fm.TextField({
		       allowBlank: false
		   })
		},{
		   header: "采食量单位",
		   dataIndex: '采食量单位',
		   width: 70,
		   align: 'right',
		   editor:  new fm.TextField({
		       allowBlank: false
		   })
		},{
		   header: "采食系数",
		   dataIndex: '采食系数',
		   width: 95,
		   editor:  new fm.TextField({
		       allowBlank: false
		   })
		},{
		   header: "换算比率",
		   dataIndex: '换算比率',
		   width: 95,
		   editor:  new fm.TextField({
		       allowBlank: false
		   })
		},{
		   header: "备注",
		   dataIndex: '备注',
		  // width: 95,
		   editor:  new fm.TextField({
		       allowBlank: false
		   })
		}
    ]);

    // by default columns are sortable
    cm.defaultSortable = true;

    var grid = new Ext.grid.EditorGridPanel({
        store: store,
	cm:cm, sm : sm ,
	bbar: [
		{
		    text: '添加',
		    "iconCls":"icon-add",
		    handler : function(){
			var p = new Plant({
			    id: newid(),
			    '单位': '新单位',
			    '采食量单位': '',
			    '采食系数': '',
			    '换算比率':'',
			    '备注': ''
			});
			newNodes[p.id]	= true;
			grid.stopEditing();
			var pos	= store.getCount();
			store.insert(pos , p);
			grid.startEditing(pos, 1);
		    }
		}, '-',
		{
		    text: '删除',
		    "iconCls":"icon-delete",
		    handler : function(){
			Ext.each(sm.getSelections(), function(node){	
				Ext.MessageBox.confirm('提示', '您确定要删除第'+ node.data.id +'条吗?', function(ret){
					if( ret !== 'yes' ){
						return false;
					}
					if( newNodes[node.id] ){
						delete newNodes[node.id];
					}
					store.remove(node);
					if( !newNodes[node.id] ){
						delNodes.push(node.data.id);
					}
				});
			});
		    }
		}, '->',
		{
		    text: '保存',
		    "iconCls":"icon-save",
		    handler : function(){
			var data	= [];
			Ext.each(store.getModifiedRecords(), function(node){
				data.push(node.data);
			});
			var newdata	= [];
			for(var i in newNodes){
				newdata.push(store.getById(i).data);
			}
			if( !data.length  && !newdata.length && !delNodes.length ){
				Ext.MessageBox.alert("提示","数据未修改！")
				return ;
			}
			loadmask.show();
			Ext.Ajax.request({
			   url: 'units.php?act=save',
			   success: function(response, options ){
				loadmask.hide();
				if(response.responseText !='ok'){
					Ext.MessageBox.alert("提示","网络异常,保存数据失败,请联系管理员！", function(){
						alert(response.responseText);
						loadmask.show();
					})
				}else{
					Ext.MessageBox.alert("提示","保存完毕！", function(){
						store.reload();
						newNodes	= {};
						delNodes	= [];
					})
				}
			   },
			   failure: function(response, options){
				Ext.MessageBox.alert("提示","网络异常,保存数据失败,请联系管理员！")
				//loadmask.hide();
			   },
			   params: { 
					insert: Ext.encode(newdata),
					update: Ext.encode(data),
					del: Ext.encode(delNodes)
				}
			});
		    }
		}, '-',
		{
		    text: '重载数据',
		    iconCls:'icon-refresh',
		    handler : function(){
			viewport.disable();
			store.reload({
				callback: function(){
					viewport.enable();
					newNodes	= {};
					delNodes	= [];
				}
			});
		    }
		}
	],

        stripeRows: true,
        height: 440,
       // title:'单位表',
	region:'center' // for viewport
	//style:"margin:10px"
    });

	var viewport = new Ext.Viewport({
		layout:'border',
		items:[
			 grid
		]
	});
	viewport.disable();
	store.load({callback: function(){
		viewport.enable();
	}});
	var newNodes	= {};
	var delNodes	= [];
			var loadmask = new Ext.LoadMask( document.body ,{
				msg:'保存数据中...', removeMask:true
			});
function newid(){
	var nid	= 1;
	store.each(function(node){
		var id = parseInt(node.data.id);
		if( id >= nid ){
			nid	= id + 1;
		}
	});
	return nid;
}

}catch(err){
	alert(err)
}

});
</script>


{-include file=$smarty.const.B_DIR|cat:'/src/_footer.htm'-}